﻿namespace KeRui.Json
{
    using System;
    using System.Collections.ObjectModel;

    internal class MemberMappingCollection : KeyedCollection<string, MemberMapping>
    {
        public void AddMapping(MemberMapping memberMapping)
        {
            if (base.Contains(memberMapping.MappingName))
            {
                if (memberMapping.Ignored)
                {
                    return;
                }
                MemberMapping item = base[memberMapping.MappingName];
                if (!item.Ignored)
                {
                    throw new JsonSerializationException(string.Format("A member with the name '{0}' already exists on {1}. Use the JsonPropertyAttribute to specify another name.", memberMapping.MappingName, memberMapping.Member.DeclaringType));
                }
                base.Remove(item);
            }
            base.Add(memberMapping);
        }

        protected override string GetKeyForItem(MemberMapping item)
        {
            return item.MappingName;
        }
    }
}

